/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月3日 上午9:15:01
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.cmpt.mybatis;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.scripting.LanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.session.Configuration;

/**   
 * @ClassName:  CustomLanguage
 * @author: 迷蝶
 * @date:   2018年1月3日 上午9:15:01 
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class CustomInLanguage extends XMLLanguageDriver implements LanguageDriver {

    private static final Pattern IN_PATTERN = Pattern.compile("\\(#\\{(\\w+)\\}\\)");
    
    @Override
    public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) {

        Matcher matcher = IN_PATTERN.matcher(script);
        if (matcher.find()) {
            script = matcher.replaceAll("<foreach collection=\"$1\" item=\"_item\" open=\"(\" separator=\",\" close=\")\" >#{_item}</foreach>");
        }
        script = "<script>" + script + "</script>";
        SqlSource sqlSource = super.createSqlSource(configuration, script, parameterType);
        return sqlSource;
    }
}
